package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.utils.threadlocal.ThreadLocalUtils;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.mapper.WmNewsMaterialMapper;
import com.usian.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WmMaterialServiceImpl extends BaseServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FastDFSClient client;

    @Value("${fdfs.url}")
    private String baseUrl;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    //图片上传
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //判断图片是否为空
        if (multipartFile == null) return paramError;
        try {
            //上传图片到虚拟机
            String s = client.uploadFile(multipartFile);
            //创建对象给属性赋值
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUrl(s);
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setUserId(ThreadLocalUtils.getUser());
            wmMaterial.setType((short) 0);
            wmMaterial.setCreatedTime(new Date());
            //添加到数据库
            save(wmMaterial);
            //拼接图片路径
            wmMaterial.setUrl(baseUrl + s);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //提示上传成功信息
        return ResponseResult.okResult("上传成功");
    }

    //查询列表
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //判断参数是否为空
        if (dto == null) return paramError;
        //获取userid
        Integer userId = ThreadLocalUtils.getUser();
        //判断id
        if (userId == null) return needLogin; //需登录后操作
        //不能简写判断 需要分开判断所以这里使用LambdaQueryWrapper
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        //比较自媒体用户id和传的id
        wrapper.eq(WmMaterial::getUserId, userId);
        //判断收藏是否为空 并和实体类传参进行比较
        if (dto.getIsCollection() != null) wrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        //根据创建时间降序
        wrapper.orderByDesc(WmMaterial::getCreatedTime);
        //调用BaseServiceImpl公共方法进行分页
        ResponseResult pageResult = getPageResult(dto, wrapper);
        //获取对象 强转成集合类型 下面要进行拼接路径
        List<WmMaterial> data = (List<WmMaterial>) pageResult.getData(); //集合对象
        //data.forEach(d -> d.setUrl(baseUrl + d.getUrl()));//接口回调
        List<Object> collect = data.stream().map(wmMaterial -> wmMaterial.resetUrl(baseUrl + wmMaterial.getUrl())).collect(Collectors.toList());
        List<String> collect1 = data.stream().map(e -> baseUrl + e.getUrl()).collect(Collectors.toList());
        pageResult.setData(data);
        return pageResult;
    }

    //删除图片
    @Override
    public ResponseResult delPicture(Integer id) {
        //判断id
        if (id == null) return paramError;
        //根据id查询一个对象
        WmMaterial material = getById(id);
        if (material == null) return noData;
        //查数量 查询中间表自媒体的id
        Integer count = wmNewsMaterialMapper.selectCount(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getMaterialId, id));
        if (count > 0) return ResponseResult.okResult(0, "该资源被引用，不能删除");//请求成功 但未能删除
        client.delFile(material.getUrl()); //先删文件 路径
        removeById(id); //删表里的数据
        return okResult;
    }

    //收藏与取消图片 收藏1 取消收藏0
    @Override
    public ResponseResult updateStatus(Integer id, Short type) {
        //判断id是否为空
        if (id == null) return paramError;
        //创建对象
        WmMaterial wmMaterial = new WmMaterial();
        //传入id 根据id来收藏或取消
        wmMaterial.setId(id);
        //type常量类声明
        wmMaterial.setIsCollection(type);
        //修改
        updateById(wmMaterial);
        return okResult;
    }
}
